package com.whfc.ms.api.sys.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fuum.dto.SysDictDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.param.SysDictDataAddParam;
import com.whfc.fuum.param.SysDictDataEditParam;
import com.whfc.fuum.param.SysDictTypeAddParam;
import com.whfc.fuum.param.SysDictTypeEditParam;
import com.whfc.ms.api.sys.service.MsSysDictService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 数据字典
 *
 * @author guodong_sun
 * @Description:
 * @date 2019年8月21日
 */
@RestController
@RequestMapping("/ms/api/sys/dict")
public class SysDictController extends BaseController {

    @Autowired
    private MsSysDictService msSysDictService;

    /**
     * 获取字典类型列表
     *
     * @param user
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequiresPermissions(value = {"sys/dict"})
    @RequestMapping("/type/list")
    public Result typeList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                           @RequestParam("pageNum") Integer pageNum,
                           @RequestParam("pageSize") Integer pageSize) {
        logger.info("获取字典表类型，loginer:{}", user.getUsername());
        PageData<SysDictDTO> pageData = msSysDictService.typeList(pageNum, pageSize);
        return ResultUtil.success(pageData);
    }

    /**
     * 增加字典类型
     *
     * @param user
     * @param request
     * @return
     */
    @RequiresPermissions(value = {"sys/dict"})
    @RequestMapping("/type/add")
    public Result addType(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                          @Validated @RequestBody SysDictTypeAddParam request) {
        logger.info("添加字典表类型，loginer:{},参数：{}", user.getUsername(), request.toString());
        msSysDictService.addType(request);
        return ResultUtil.success();
    }

    /**
     * 编辑字典表类型
     *
     * @param user
     * @param request
     * @return
     */
    @RequiresPermissions(value = {"sys/dict"})
    @RequestMapping("/type/edit")
    public Result editType(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                           @Validated @RequestBody SysDictTypeEditParam request) {
        logger.info("编辑字典表类型，loginer:{},参数：{}", user.getUsername(), request.toString());
        msSysDictService.editType(request);
        return ResultUtil.success();
    }

    /**
     * 删除字典表类型
     *
     * @param user
     * @param id
     * @return
     */
    @RequiresPermissions(value = {"sys/dict"})
    @RequestMapping("/type/del/{id}")
    public Result delType(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @PathVariable("id") Integer id) {
        logger.info("删除字典表类型，loginer:{},参数id：{}", user.getUsername(), id);
        msSysDictService.delType(id);
        return ResultUtil.success();
    }


    /**
     * 查询数据字典
     *
     * @param user
     * @param sysDictTypeId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequiresPermissions(value = {"sys/dict"})
    @RequestMapping("/list")
    public Result listDictData(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                               @RequestParam("sysDictTypeId") Integer sysDictTypeId,
                               @RequestParam("pageNum") Integer pageNum,
                               @RequestParam("pageSize") Integer pageSize) {
        logger.info("查询数据字典，loginer:{}，参数sysDictTypeId:{},pageNum:{},pageSize:{}", user.getUsername(), sysDictTypeId,
                pageNum, pageSize);
        PageData<SysDictDTO> pageData = msSysDictService.listDictData(sysDictTypeId, pageNum, pageSize);
        return ResultUtil.success(pageData);
    }

    /**
     * 添加数据字典
     *
     * @param user
     * @param request
     * @return
     */
    @RequiresPermissions(value = {"sys/dict"})
    @RequestMapping("/add")
    public Result addDictData(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                              @Validated @RequestBody SysDictDataAddParam request) {
        logger.info("添加数据字典，loginer:{}，参数:{}", user.getUsername(), request.toString());
        msSysDictService.addDictData(request);
        return ResultUtil.success();
    }

    /**
     * 编辑数据字典
     *
     * @param user
     * @param request
     * @return
     */
    @RequiresPermissions(value = {"sys/dict"})
    @RequestMapping("/edit")
    public Result editDictData(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                               @Validated @RequestBody SysDictDataEditParam request) {
        logger.info("编辑数据字典，loginer:{}，参数:{}", user.getUsername(), request.toString());
        msSysDictService.editDictData(request);
        return ResultUtil.success();
    }

    /**
     * 删除数据字典
     *
     * @param user
     * @param id
     * @return
     */
    @RequiresPermissions(value = {"sys/dict"})
    @RequestMapping("/del/{id}")
    public Result delDictData(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @PathVariable("id") Integer id) {
        logger.info("删除字典数据，loginer:{},参数id：{}", user.getUsername(), id);
        msSysDictService.delDictData(id);
        return ResultUtil.success();
    }

    /**
     * 根据父节点code获取字典数据
     *
     * @param user
     * @param code
     * @return
     */
    @GetMapping("/getChildListByParentCode")
    public Result getChildListByParentCode(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestParam("code") String code) {
        logger.info("获取字典{}的数据，loginer:{}", code, user.getUsername());
        SysDictDTO dictDTO = msSysDictService.getDictDataByTypeCode(code);
        return ResultUtil.success(dictDTO);
    }
}
